/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.fdm.tst;

import java.security.Principal;
import java.security.PrivilegedAction;
import javax.security.auth.Subject;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
import org.fdm.tst.security.AstjSecurityConfiguration;
import org.fdm.tst.security.AstjUser;
import org.fdm.tst.security.CmdPromptLoginCallbackHandler;

/**
 *
 * @author fdm
 */
public class Main {
    
    public static void main(String[] args) {
        
        Configuration.setConfiguration(new AstjSecurityConfiguration());
        
        try{
            LoginContext ctx = 
                    new LoginContext("MainApp", new CmdPromptLoginCallbackHandler());
            ctx.login();
            final Subject subject = ctx.getSubject();
            System.out.println("Login success");
            
            Subject.doAs(subject, new PrivilegedAction<Object>() {

                @Override
                public Object run() {
                    for(Principal p : subject.getPrincipals(AstjUser.class)){
                        AstjUser au = (AstjUser) p;
                        System.out.println(au.getDisplayName());
                    }
                    doSomeAction();
                    return null;
                }
            });
            
            ctx.logout();
            
        } catch(LoginException ex){
            System.out.println(ex);
        }
        
    }
    
    private static void doSomeAction(){
        System.out.println("abyrvalg");
    }
    
}
